/***************************************************************************************************
Copyright 2009 - 2010 Harshana Eranga Martin, Dishan Metihakwala, Rajeev Sampath, Chathura Randika

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
****************************************************************************************************/
package org.epzilla.accumulator.client;

import java.util.Random;
import java.io.FileWriter;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: Dishan
 * Date: Mar 7, 2010
 * Time: 9:01:49 AM
 * To change this template use File | Settings | File Templates.
 */
public class EventTriggerGenerator {
    private static final char[] symbols = new char[36];
    private static final Random random = new Random();
    private static char[] buf = new char[20];

    static {
        for (int idx = 0; idx < 10; ++idx)
            symbols[idx] = (char) ('0' + idx);
        for (int idx = 10; idx < 36; ++idx)
            symbols[idx] = (char) ('a' + idx - 10);
    }

    public static String nextString() {
        for (int idx = 0; idx < buf.length; ++idx)
            buf[idx] = symbols[random.nextInt(symbols.length)];
        return new String(buf);
    }

    //For teting only-Dishan
    public static void main(String[] args) {
        String temp;
        for (int i = 0; i < 10; i++) {
            temp = getNextTrigger();
        }
    }


    public static String getNextEvent() {
        StringBuilder writer = new StringBuilder();
        try {
            writer.append("Title");
            writer.append(',');
            writer.append("CarModel");
            writer.append(',');
            writer.append("Color");
            writer.append(',');
            writer.append("Year");
            writer.append('\n');

            writer.append("CarDetails");
            writer.append(',');
            writer.append(Const.WORDS[random.nextInt(Const.WORDS.length)]);
            writer.append(',');
            writer.append(Const.COLOR[random.nextInt(Const.COLOR.length)]);
            writer.append(',');
            writer.append(Const.YEAR[random.nextInt(Const.YEAR.length)]);
//            writer.append('\n');

        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return writer.toString();
//        return writer.lastIndexOf("\n") == (writer.length() - 1)? writer.substring(0, writer.length() - 1): writer.toString();
    }

//    "select CarDetails.CarModel where CarDetails.Year=1980  output as Details"

    public static String getNextTrigger() {
        StringBuilder writer = new StringBuilder();
        try {
            writer.append("SELECT");
            writer.append(" ");
            writer.append("CarDetails");
            writer.append('.');
            writer.append(Const.PROPERTY[random.nextInt(Const.PROPERTY.length)]);
            writer.append(" ");
            writer.append("WHERE");
            writer.append(" ");
            writer.append("CarDetails");
            writer.append('.');
            int temp = random.nextInt(Const.PROPERTY.length);
            writer.append(Const.PROPERTY[temp]);
            writer.append('=');
            switch (temp) {
                case 0:
                    writer.append(Const.WORDS[random.nextInt(Const.WORDS.length)]);
                    break;
                case 1:
                    writer.append(Const.YEAR[random.nextInt(Const.YEAR.length)]);
                    break;
                case 2:
                    writer.append(Const.COLOR[random.nextInt(Const.COLOR.length)]);
                    break;
            }
            writer.append(" ");
            writer.append("OUTPUT AS");
            writer.append(" ");
            writer.append("CarDetailsResults");
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return writer.toString();
    }


}
